﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcShop.Models;
using MvcShop.Models.Repositories;
using MvcShop.Core.Architecture.Cache;
using MvcShop.Models.Services;
using System.Web.Mvc;

namespace MvcShop.MVC.Services
{
    public class AdService : IAdService
    {
        public AdService(IAdPositionRepository<AdPosition> adPRepository, 
            IAdsRepository<Ads> adsRepository, 
            IMsCache msCache, 
            string mark)
        {
            this._mark = mark;
            this._msCache = msCache;
            if (!this._msCache.TryGet("AdPList", out _adPProvider))
            {
                try
                {
                    _adPProvider = adPRepository.FindAll();
                    var _ExpireTime = DateTime.Now.AddDays(1);//指定1天后过期
                    this._msCache.Set<IEnumerable<AdPosition>>("AdPList", _adPProvider, _ExpireTime);
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
            }

            if (!this._msCache.TryGet("Adses", out _adsProvider))
            {
                try
                {
                    _adsProvider = adsRepository.FindAll();
                    var _ExpireTime = DateTime.Now.AddDays(1);//指定1天后过期
                    this._msCache.Set<IEnumerable<Ads>>("Adses", _adsProvider, _ExpireTime);
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
            }

            var apType = (from a in _adPProvider
                      where a.Mark == mark
                      select a.Type).FirstOrDefault();
            if (apType != null)
            {
                this._type = apType;
            }
        }

        private IEnumerable<Ads> _adsProvider;
        private IEnumerable<AdPosition> _adPProvider;
        private IMsCache _msCache;
        private string _mark;
        private string _type;

        /// <summary>
        /// 获得该广告位下的单个广告
        /// </summary>
        /// <returns></returns>
        public string GetSingleAdHtmlContent(string siteUrl)
        {
            StringBuilder sb = new StringBuilder();

            var ad = (from a in this._adsProvider
                      where a.Mark == this._mark && DateTime.Now <= a.EndDate && a.IsUsed
                      select a).FirstOrDefault();

            if (this._type.ToLower() == AdType.Text.ToString().ToLower())
            {
                if (ad == null)
                {
                    sb.Append("<a href=\"" + siteUrl + "/ads/freead?mark=" + this._mark + "\" target=\"_blank\">广告位招租中</a>");
                }
                sb.Append("<a href=\"" + ad.LinkUrl + "\" target=\"_blank\">" + ad.Text + "</a>");
            }
            else if (this._type.ToLower() == AdType.Image.ToString().ToLower())
            {
                if (ad == null)
                {
                    sb.Append("<A href=\"" + siteUrl + "/ads/freead?mark=" + this._mark + "\" target=\"_black\">" +
                    "<img src=\"" + siteUrl + "uploads/ads/images/picad_zz.gif\" class=\"adimg\" border=\"0\"/></a>");
                }
                else
                {
                    sb.Append("<a href=\"" + ad.LinkUrl + "\" target=\"_black\"><img src=\"" +siteUrl+
                        ad.ImageUrl + "\" class=\"adimg\" border=\"0\"/></a>");
                }
            }
            return sb.ToString();

        }

        /// <summary>
        /// 获取该广告位下的所有广告以li标签显示
        /// </summary>
        /// <param name="num">要加载的总数,如果为空则加载所有</param>
        /// <returns></returns>
        public string GetListAdHtmlContent(string siteUrl,int? num)
        {
            StringBuilder sb = new StringBuilder();

            var items = (from a in _adsProvider
                        where a.Mark == this._mark && DateTime.Now <= a.EndDate && a.IsUsed
                         select a).Take(num ?? 100000);

            if (this._type.ToLower() == AdType.Text.ToString().ToLower())
            {
                if (items.Count() == 0)
                {
                    sb.Append("<li><a href=\"" + siteUrl + "ads/freead?mark=" + this._mark + "\" target=\"_blank\">广告位招租中</a></li>");
                }
                else
                {
                    foreach (var item in items)
                    {
                        sb.Append("<li><a href=\"" + item.LinkUrl + "\" target=\"_blank\">" +
                            item.Text + "</a></li>");
                    }
                }
            }
            else if (this._type.ToLower() == AdType.Image.ToString().ToLower())
            {
                if (items.Count() == 0)
                {

                    sb.Append("<li><a href=\"" + siteUrl + "ads/freead?mark=" + this._type + "\" target=\"_black\">" +
                    "<img src=\"" + siteUrl + "uploads/ads/images/picad_zz.gif\" class=\"adimg\" border=\"0\"/></a></li>");
                }
                else
                {
                    foreach (var item in items)
                    {
                        sb.Append("<li><a href=\"" + item.LinkUrl + "\" target=\"_blank\">" +
                            "<img src=\"" + siteUrl + item.ImageUrl + "\" class=\"adimg\" border=\"0\"/></a></li>");
                    }
                }
            }
            return sb.ToString();

        }
    }
}

